--[[		lolTip
	        by Lolzen
		Credits: 
	*Aezay for TipTac, TipTacTalents
]]--	*Cargor (EU-Nozdormu) for Helping me whenever i'm freaking out

------------------------------------------------------------------------------
-- Backdrop 
------------------------------------------------------------------------------
GameTooltip:SetBackdrop{ 
	bgFile = "Interface\\ChatFrame\\ChatFrameBackground", 
	edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border", 
	tile = true, 
	tileSize = 8, 
	edgeSize = 16, 
	insets = { 
		left = 4, 
		right = 4, 
		top = 4, 
		bottom = 4 
		} 
	}

GameTooltip:SetScript("OnShow", function() 
    GameTooltip:SetBackdropColor(0, 0, 0, 1) 
    GameTooltip:SetBackdropBorderColor(0.4, 0.4, 0.4) 
    if GameTooltip:GetWidth() < 100 then 
        GameTooltip:SetWidth(100)
    end 
end); 


------------------------------------------------------------------------------
-- Anchor & Scale
------------------------------------------------------------------------------
hooksecurefunc("GameTooltip_SetDefaultAnchor", function (tooltip, parent)
	tooltip:SetOwner(parent,"ANCHOR_NONE")
	tooltip:SetPoint("TOPRIGHT",UIParent,"TOPRIGHT", -15, -200)
	tooltip:SetScale(1)
	tooltip.default = 1
end);

------------------------------------------------------------------------------
-- Color names in their ClassColor
------------------------------------------------------------------------------
local Real_GameTooltip_UnitColor = GameTooltip_UnitColor
function GameTooltip_UnitColor(unit)
	if UnitIsPlayer(unit) then 
		local color = RAID_CLASS_COLORS[select(2,UnitClass(unit))]
		return color.r, color.g, color.b
	elseif UnitIsTapped(unit) and not UnitIsTappedByPlayer(unit) or not UnitIsConnected(unit) or UnitIsDead(unit) then
		local color = {r = 0.6, g = 0.6, b = 0.6}
		return color.r, color.g, color.b
	elseif not isPlayerOrPet then
		local color = UnitReactionColor[UnitReaction(unit, "player")]
		return color.r, color.g, color.b
	else
		return Real_GameTooltip_UnitColor(unit)
	end
end

------------------------------------------------------------------------------
-- Talents (+Slight hackish way to hide PvPline)
------------------------------------------------------------------------------
local _G = getfenv(0)

local points = {}, {}
local unit, val
local TALENTS_PREFIX = "|cffffd700Talents:|r |cffffffff "

local function GatherTalents(inspect)
	for i = 1, 3 do
		points[i] = select(3,GetTalentTabInfo(i,inspect))
		if (i == 1) or (points[i] > points[val]) then
			val = i
		end
	end
	val = GetTalentTabInfo(val,inspect).." ("..points[1].."/"..points[2].."/"..points[3]..")"
	for i = 2, GameTooltip:NumLines() do
		if ((_G["GameTooltipTextLeft"..i]:GetText() or ""):find("^"..TALENTS_PREFIX)) then
			_G["GameTooltipTextLeft"..i]:SetText(TALENTS_PREFIX..val)
			GameTooltip:Show()
			break
		end
	end
end;

local f = CreateFrame("Frame")
f:SetScript("OnEvent",function(self,event,...)
	f:UnregisterEvent("INSPECT_TALENT_READY")
	GatherTalents(1)
end);

GameTooltip:HookScript("OnTooltipSetUnit", function(self,...)
	unit = select(2,self:GetUnit())
	if not unit then
		return nil
	end
	if (UnitIsPlayer(unit)) and UnitLevel(unit) > 9 or UnitLevel(unit) == -1 then
		if UnitIsUnit(unit,"player") and UnitIsPVP(unit) then
			for i = 2, GameTooltip:NumLines() do
				if (_G["GameTooltipTextLeft"..i]:GetText():find(PVP_ENABLED)) then
					_G["GameTooltipTextLeft"..i]:SetText(TALENTS_PREFIX.." Loading...")
					GatherTalents()
				end
			end
		elseif UnitIsUnit(unit,"player") then
			self:AddLine(TALENTS_PREFIX.." Loading...")
			GatherTalents()
		else
			if CheckInteractDistance(unit,1) and CanInspect(unit) then
				f:RegisterEvent("INSPECT_TALENT_READY");
				NotifyInspect(unit)
			end
			if CheckInteractDistance(unit,1) and CanInspect(unit) and UnitIsPVP(unit) then
				for i = 2, GameTooltip:NumLines() do
					if (_G["GameTooltipTextLeft"..i]:GetText():find(PVP_ENABLED)) then
						_G["GameTooltipTextLeft"..i]:SetText(TALENTS_PREFIX.." Loading...")
					end
				end
			elseif CheckInteractDistance(unit,1) and CanInspect(unit) then
				self:AddLine(TALENTS_PREFIX.." Loading...")
			end
		end
	end
end);

------------------------------------------------------------------------------
--Targetline
------------------------------------------------------------------------------
unit = select(2, GameTooltip:GetUnit())

local ureaction, targetline, info
local ClassColors = {}
for class, color in pairs(RAID_CLASS_COLORS) do
	ClassColors[class] = ("|cff%.2x%.2x%.2x"):format(color.r*255,color.g*255,color.b*255)
end

local function GetTargetLine(unit)
	if UnitIsUnit(unit, "player") then
		return "|cffffffff[YOU]|r" 
	elseif UnitIsPlayer(unit) then
		return ClassColors[select(2, UnitClass(unit))]..UnitName(unit).."|r"
	else
		ureaction = UnitReactionColor[UnitReaction(unit, "player")]
	    return string.format("|cff%02x%02x%02x%s|r", ureaction.r*255, ureaction.g*255, ureaction.b*255, UnitName(unit))
	end
end;

GameTooltip:HookScript("OnTooltipSetUnit", function(self) 
	unit = select(2,self:GetUnit())
	if not unit or not UnitExists(unit.."target") then
		targetline = nil
		return nil
	end
	GameTooltip:AddLine("Target: "..GetTargetLine(unit.."target"))
	targetline = GameTooltip:NumLines() 
end); 

local frame = CreateFrame("Frame")
frame.lastupdate = 0
frame:SetScript("OnUpdate", function(self, elapsed)
	self.lastupdate = self.lastupdate + elapsed
	if self.lastupdate > 0.1 then
		unit = select(2, GameTooltip:GetUnit())
		if not unit then 
			return nil 
		end
		if UnitExists(unit.."target") then
			if targetline then
				_G['GameTooltipTextLeft'..targetline]:SetText("Target: "..GetTargetLine(unit.."target"))
				_G['GameTooltipTextLeft'..targetline]:Show()
				_G['GameTooltipTextLeft'..GameTooltip:NumLines()]:SetText(" ")
				GameTooltip:Show()
			else
				GameTooltip:AddLine("Target: "..GetTargetLine(unit.."target"))
				targetline = GameTooltip:NumLines() -1
				GameTooltip:Show()
			end
		elseif targetline then
			_G['GameTooltipTextLeft'..targetline]:Hide()
			GameTooltip:Show()
		end
	end
end);

------------------------------------------------------------------------------
--Lvl, Race & Class Display, Guild Coloring
------------------------------------------------------------------------------
local diffcolor, level, leveltext

GameTooltip:HookScript("OnTooltipSetUnit", function(self)
	unit = select(2,self:GetUnit())
	if not unit then
		return nil
	end
	-- Player tooltip text
	if UnitIsPlayer(unit) then
		GameTooltipTextLeft1:SetText(UnitName(unit)) --Comment this out if you want Player Titles (eg. ImbaRoxx0r, Champion of the Naaru)
			level = UnitLevel(unit)
			diffcolor = GetDifficultyColor(level)
		if level == "??" or level == -1 then 
			leveltext = "|cffff0000??|r" 
		else
			leveltext = string.format("|cff%02x%02x%02x%d",diffcolor.r*255,diffcolor.g*255,diffcolor.b*255,level).."|r"
		end
		for i = 2, GameTooltip:NumLines() do
			if (_G["GameTooltipTextLeft"..i]:GetText():find("^".."Level")) then
				_G["GameTooltipTextLeft"..i]:SetText(leveltext.." "..UnitRace(unit).." "..UnitClass(unit))
				break
			end
		end
		if UnitIsAFK(unit) then
			local lineText = GameTooltipTextLeft1:GetText()
			if (lineText and string.sub(lineText, 1, string.len(CHAT_FLAG_AFK)) ~= CHAT_FLAG_AFK) then
				GameTooltipTextLeft1:SetText(CHAT_FLAG_AFK..lineText)
			end
		end
		if UnitIsDND(unit) then
			local lineText = GameTooltipTextLeft1:GetText()
			if (lineText and string.sub(lineText, 1, string.len(CHAT_FLAG_DND)) ~= CHAT_FLAG_DND) then
				GameTooltipTextLeft1:SetText(CHAT_FLAG_DND..lineText)
			end
		end
		-- Guild coloring
		if IsInGuild() and GameTooltipTextLeft2:GetText():find("^" .. GetGuildInfo("player")) then
			GameTooltipTextLeft2:SetTextColor(0.2, 0.8, 8)
		end
	-- Mob tooltip text
	else
		level = UnitLevel(unit) 
		diffcolor = GetDifficultyColor(level)
		leveltext = string.format("|cff%02x%02x%02x%d",diffcolor.r*255,diffcolor.g*255,diffcolor.b*255,level).."|r"
		if UnitClassification(unit) == "worldboss" then
			leveltext = string.format("|cffff0000Boss|r")
		elseif UnitClassification(unit) == "rareelite" then
			leveltext = string.format("|cff%02x%02x%02x%d",diffcolor.r*255,diffcolor.g*255,diffcolor.b*255,level).."+|r Rare"
			if level == -1 then
				leveltext = "|cffff0000??+|r Rare"
			end
		elseif UnitClassification(unit) == "elite" then
			leveltext = string.format("|cff%02x%02x%02x%d",diffcolor.r*255,diffcolor.g*255,diffcolor.b*255,level).."+|r"
			if level == -1 then
				leveltext = "|cffff0000??+|r"
			end
		elseif UnitClassification(unit) == "rare" then
			leveltext = string.format("|cff%02x%02x%02x%d",diffcolor.r*255,diffcolor.g*255,diffcolor.b*255,level).."|r Rare"
			if level == -1 then
				leveltext = "|cffff0000??|r Rare"
			end
		end
		for i = 2, GameTooltip:NumLines() do
			if (_G["GameTooltipTextLeft"..i]:GetText():find("^".."Level")) then
				_G["GameTooltipTextLeft"..i]:SetText(leveltext.." "..UnitCreatureType(unit))
				break
			end
		end
	end
end);

------------------------------------------------------------------------------
-- HealthBar
------------------------------------------------------------------------------
GameTooltip:HookScript("OnTooltipSetUnit", function(self)
	if GameTooltipStatusBar then
		self:AddLine(" ")
		GameTooltipStatusBar:ClearAllPoints()
		GameTooltipStatusBar:SetPoint("BOTTOMLEFT", 8, 9)
		GameTooltipStatusBar:SetPoint("BOTTOMRIGHT", -8, 9)
		updateTooltipHealth(UnitHealth("mouseover"), UnitHealthMax("mouseover"))
	end
end);

function HealthBar_OnValueChanged(value)
	if not value then
		return nil
	end
	local r, g, b
	local min, max = this:GetMinMaxValues()
	if ((value < min) or (value > max)) then
		return nil
	end
	if ((max - min) > 0) then
		value = (value - min) / (max - min)
	else
		value = 0
	end
	GameTooltipStatusBar:SetStatusBarColor(0.3, 0.9, 0.3, 1)
	GameTooltipStatusBar:SetStatusBarTexture("Interface\\TargetingFrame\\UI-StatusBar")
	unit = select(2, GameTooltip:GetUnit())
	if unit then
		updateTooltipHealth(UnitHealth(unit), UnitHealthMax(unit))
	end
end

-- TextValues for the HealthBar 
function updateTooltipHealth(min, max)
	if not GameTooltipStatusBar.text then
		local text = GameTooltipStatusBar:CreateFontString(nil, "OVERLAY")
		text:SetFont("Fonts\\FRIZQT__.TTF", 12, "OUTLINE")
		text:SetPoint("CENTER", GameTooltipStatusBar)
		GameTooltipStatusBar.text = text
	end
	if (max == 100) then
		GameTooltipStatusBar.text:SetText(string.format("%.0f%%",min/max*100))
	else
		GameTooltipStatusBar.text:SetText(min.."/"..max)
	end
end

------------------------------------------------------------------------------
-- Other Tooltips
------------------------------------------------------------------------------
local function TipsOnShow(self)
	self:SetBackdropColor(0, 0, 0, 1)
	self:SetBackdropBorderColor(0.4, 0.4, 0.4)	
end

-- Itemref
ItemRefTooltip:SetBackdrop{ 
	bgFile = "Interface\\ChatFrame\\ChatFrameBackground", 
	edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border", 
	tile = true, 
	tileSize = 8, 
	edgeSize = 16, 
	insets = { 
		left = 4, 
		right = 4, 
		top = 4, 
		bottom = 4 
		} 
	}
ItemRefTooltip:SetScript("OnShow",TipsOnShow)

-- ShoppingTooltip1
ShoppingTooltip1:SetBackdrop{ 
	bgFile = "Interface\\ChatFrame\\ChatFrameBackground", 
	edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border", 
	tile = true, 
	tileSize = 8, 
	edgeSize = 16, 
	insets = { 
		left = 4, 
		right = 4, 
		top = 4, 
		bottom = 4 
		} 
	}
ShoppingTooltip1:SetScript("OnShow",TipsOnShow)

-- ShoppingTooltip2
ShoppingTooltip2:SetBackdrop{ 
	bgFile = "Interface\\ChatFrame\\ChatFrameBackground", 
	edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border", 
	tile = true, 
	tileSize = 8, 
	edgeSize = 16, 
	insets = { 
		left = 4, 
		right = 4, 
		top = 4, 
		bottom = 4 
		} 
	}
ShoppingTooltip2:SetScript("OnShow",TipsOnShow)
